package com.bst.system.controller;

import java.util.List;

import com.bst.common.core.domain.entity.SysDept;
import com.bst.common.utils.poi.EasyExcelUtil;
import com.bst.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bst.common.annotation.Log;
import org.springframework.stereotype.Controller;
import com.bst.common.enums.BusinessType;
import com.bst.system.domain.SysUserDept;
import com.bst.system.service.ISysUserDeptService;
import com.bst.common.core.controller.BaseController;
import com.bst.common.core.domain.AjaxResult;
import com.bst.common.utils.poi.ExcelUtil;
import com.bst.common.core.page.TableDataInfo;

/**
 * 用户科室Controller
 * 
 * @author laoliu
 * @date 2024-11-25
 */
@Controller
@RequestMapping("/base/system/userDept")
public class SysUserDeptController extends BaseController
{
    private String prefix = "system/userDept";

    @Autowired
    private ISysUserDeptService sysUserDeptService;
    @Autowired
    private ISysDeptService sysDeptService;

    @GetMapping()
    public String userDept()
    {
        return prefix + "/userDept";
    }

    /**
     * 查询用户科室列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo selectDeptsByUserId(Long userId)
    {
        List<SysDept> depts = sysDeptService.selectDeptsByUserId(userId);
        return getDataTable(depts);
    }

    /**
     * 导出用户科室列表
     */
    @Log(title = "用户科室", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysUserDept sysUserDept)
    {
        List<SysUserDept> list = sysUserDeptService.selectSysUserDeptList(sysUserDept);
        ExcelUtil<SysUserDept> util = new ExcelUtil<SysUserDept>(SysUserDept.class);
        return util.exportExcel(list, "用户科室数据");
    }

    /**
     * 新增用户科室
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户科室
     */
    @Log(title = "用户科室", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysUserDept sysUserDept)
    {
        return toAjax(sysUserDeptService.insertSysUserDept(sysUserDept));
    }

    /**
     * 修改用户科室
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        SysUserDept sysUserDept = sysUserDeptService.selectSysUserDeptByUserId(userId);
        mmap.put("sysUserDept", sysUserDept);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户科室
     */
    @Log(title = "用户科室", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysUserDept sysUserDept)
    {
        return toAjax(sysUserDeptService.updateSysUserDept(sysUserDept));
    }

    /**
     * 删除用户科室
     */
    @Log(title = "用户科室", businessType = BusinessType.DELETE)
    @GetMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Long deptId,Long userId)
    {
        return toAjax(sysUserDeptService.deleteOneSysUserDept(userId,deptId));
    }
}
